package inutan.match.action;

import inutan.common.constant.DataConstant;
import inutan.common.context.FlowContext;
import inutan.common.inner.protocol.InnerMessage;
import inutan.common.kit.InnerKit;
import inutan.match.service.MatchService;
import luban.common.Command;
import org.ricks.common.lang.Logger;
import org.ricks.ioc.anno.Action;
import org.ricks.ioc.anno.ActionMethod;
import org.ricks.ioc.anno.Autowired;
import java.util.concurrent.atomic.AtomicLong;


/**
 * @author ：demon-chen
 * 来自业务集群的消息 使用自定义消息体
 * 如果1:1 对应 ，
 */
@Action
public class MatchController {

    private static final AtomicLong l = new AtomicLong();

    @Autowired
    private MatchService matchService;
//
//    /**
//     * 网关启动 重启，通知业务节点。
//     * 业务节点告知网关，模块ID  ID  cmd  在线人数
//     * @param context
//     */
//    @ActionMethod(cmd = Command.LoginBroker)
//    public void brokerIntoGame(GameFlowContext context) {
//        LoginBroker loginBroker = new LoginBroker();
//        loginBroker.type = ConfigConstant.MODULE_ID;
//        loginBroker.serverId = ConfigConstant.ID;
//        HashSet<Short> allCmd = new HashSet<>();
//        loginBroker.bitCmd = allCmd;
//        InnerMessage innerMessage = InnerKit.createInnerMessage(Command.LoginBroker, loginBroker);
//        context.sendResponse(innerMessage);
//    }


    /**
     * 创建房间
     * @param context
     * @return
     */
    @ActionMethod(cmd = Command.CreateRoom)
    public void createRoom(FlowContext context) {
        long count = l.getAndIncrement();
        Logger.info(Thread.currentThread().getName() + "匹配服收到进入房间指令.................................  packId:" + context.getPackageId() + ", count:" + count);

        matchService.createRoom(context);

        /**
         * 路由网关，进行房间绑定
         */
        InnerMessage innerMessage = InnerKit.createInnerMessage((short) 10001);
        context.sendResponse(DataConstant.BROADCAST_MESSAGE,(short) 10001);
    }

    /**
     * 进入聊天
     * @param context
     */
    @ActionMethod(cmd = Command.EnterRoom)
    public void enterRoom(FlowContext context) {
        Logger.info(context.getUserId() +" :开始进入房间。。。。 ");
        matchService.enterRoom(context);
        context.sendResponse(InnerKit.createInnerMessage((short) 10004));
        InnerMessage enterRoomMessage = InnerKit.createInnerMessage(Command.EnterRoom);
        enterRoomMessage.state = DataConstant.BROADCAST_MESSAGE;
        context.sendResponse(DataConstant.BROADCAST_MESSAGE,Command.EnterRoom);
    }

    /**
     * 准备
     */
    @ActionMethod(cmd = Command.RoomReady)
    public void ready(FlowContext context) {

    }

    /**
     * 退出房间
     * @param context
     */
    @ActionMethod(cmd = Command.ExitRoom)
    public void exitRoom(FlowContext context) {
        Logger.info("用户退出游戏 userId:" +context.getUserId());
    }
}
